package com.wsx.rpc.resilience4j.breaker;

import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;

import java.time.Duration;
import java.util.concurrent.CompletableFuture;

/**
 * @author wangshuaixin
 * @Title: com.wsx.rpc.resilience4j.breaker.BreakerTest
 * @Description: TODO
 * @date 2018/12/06
 */
public class BreakerTest {


    public static void main(String[] args) {
        CircuitBreakerConfig breakerConfig = CircuitBreakerConfig.custom()
                .failureRateThreshold(20)
                .waitDurationInOpenState(Duration.ofMillis(2000))
                .ringBufferSizeInClosedState(2)
                .ringBufferSizeInHalfOpenState(2)
                .build();


        CircuitBreakerRegistry registry = CircuitBreakerRegistry.of(breakerConfig);

        CircuitBreaker breaker = registry.circuitBreaker("breaker");

        String result = breaker.executeSupplier(() -> "hello");
        System.out.println("result:" + result);

        breaker.executeCompletionStage(() -> new CompletableFuture<String>());
    }
}
